<?php
/**
 * @author ykm 2016/6/16
 * @describe 菜鸟快递单模板二期
 */

class logisticsmanager_ctl_admin_express_cainiao extends desktop_controller {

    public function syncTpl() {
        $this->pagedata['finder_id'] = $_GET['finder_id'];
        $this->display('admin/express/sync_tpl_cainiao.html');
    }

    public function doSyncTpl() {
	ini_set('display_errors','On');
        $rs = array();
        $channel = app::get('logisticsmanager')->model('channel')->getList('channel_id,shop_id', array('channel_type'=>'taobao', 'status'=>'true'));
	if($channel) {
            $msg = '';
            $erpApi = kernel::single('erpapi_router_request');
            $logisticsTaobao = kernel::single('logisticsmanager_waybill_taobao');
            $modelTpl = app::get('logisticsmanager')->model('express_template');
            $tplIdFilter = $dealData = array();
            $rs = $erpApi->set('logistics', $channel[0]['channel_id'])->template_syncStandardTpl();
            if($rs['rsp'] == 'succ') {
                $requestResult = true;
                foreach($rs['data'] as $data) {
                    $logistics = $logisticsTaobao->logistics($data['cp_code']);
                    if($logistics['name']) {
                    //    $data['template_name'] = $logistics['name'] . '菜鸟标准云模板';
                        $tplIdFilter[] = $data['out_template_id'];
                        $dealData['standard' . '-' . $data['out_template_id']] = $data;
                    }
                }
		$taobaoShop = app::get('ome')->model('shop')->getList('shop_id,node_id',array('node_type'=>'taobao','filter_sql'=>'node_id is not null and node_id!=""'));
                $bindShop = array();
                foreach ($taobaoShop as $key => $value) {
                    $bindShop[] = $value['shop_id'];
                }
                $hasShopId = array();
                foreach($channel as $val) {
                    if($hasShopId && in_array($val['shop_id'], $hasShopId)) {
                        continue;
                    }
		if(!in_array($val['shop_id'], $bindShop)) continue;
                    $hasShopId[] = $val['shop_id'];
                    $rs = $erpApi->set('logistics', $val['channel_id'])->template_syncUserTpl();
                    if($rs['rsp'] == 'succ') {
                        foreach($rs['data'] as $data) {
                            $tplIdFilter[] = $data['out_template_id'];
                            $dealData['user' . '-' . $data['out_template_id']] = $data;
                        }
                    } else {
                        $requestResult = false;
                        $msg .= "<br/> channel：{$val['channel_id']} 菜鸟模板同步失败,{$rs['msg']}";
                    }
                }
		
                $tplData = $modelTpl->getList('template_id, out_template_id, template_type', $tplIdFilter);
		foreach($tplData as $tVal) {
                    $tplIndex = '';
                    if($tVal['template_type'] == 'cainiao_standard') {
                        $tplIndex = 'standard' . '-' . $tVal['out_template_id'];
                    } elseif($tVal['template_type'] == 'cainiao_user') {
                        $tplIndex = 'user' . '-' . $tVal['out_template_id'];
                    }
                    if($tplIndex && $dealData[$tplIndex]) {
                        $dealData[$tplIndex]['template_id'] = $tVal['template_id'];
                    }
                }
                $updateTplId = array();
                $printTpl = kernel::single('logisticsmanager_print_tmpl');
                foreach($dealData as $dVal) {
                    $rs = $printTpl->save($dVal);

                    if($rs['rs'] == 'succ') {
                        $updateTplId[] = $rs['data']['template_id'];
                    } else {
                        if($dVal['template_id']) {
                            $updateTplId[] = $dVal['template_id'];
                            $msg .= "<br/> template_id：{$dVal['template_id']} 更新失败,{$rs['msg']}";
                        } else {
                            $msg .= "<br/> out_template_id：{$dVal['out_template_id']} 写入失败,{$rs['msg']}";
                        }
                    }
                }
                
                #删除没有涉及的菜鸟模板
                if($requestResult) {
              //      $modelTpl->db->exec("DELETE FROM sdb_logisticsmanager_express_template WHERE template_id not in(".implode(',',$updateTplId).") AND template_type in('cainiao_standard','cainiao_user') AND out_template_id!=0");

                }
            } else {
                $msg .= "<br/> 标准菜鸟模板获取失败,{$rs['msg']}。用户定义菜鸟模板需标准菜鸟模板获取成功才能使用";
            }
            $rs['msg'] = $msg;
        } else {
            $rs['msg'] = '电子面单来源没有淘宝类型，无需同步';
        }
        $rs['rsp'] = $rs['msg'] ? 'fail' : 'succ';
        echo json_encode($rs);
    }

    
}
